package ${package}.service;


import com.google.common.collect.Maps;
import com.ishansong.common.web.utils.RequestUtil;
import com.ishansong.log.Loggers;
import org.slf4j.Logger;

import javax.servlet.http.HttpServletRequest;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Author: sid
 * Date:   17/11/26 下午4:20
 */

public abstract class BaseService {


    protected final static Logger log = Loggers.get("default");

    protected Map<Object,Object> init(HttpServletRequest request, String methodName){

        Map<Object,Object> params  = Maps.newHashMap();

        params.putAll(convertParameterMap(request.getParameterMap()));

        params.put("function",methodName);

        String ip = RequestUtil.getRequestIp(request);

        params.put("ip",ip);

        return params;
    }


    private Map<String,String> convertParameterMap(Map<String,String[]> parameterMap){

        if(parameterMap == null || parameterMap.size() == 0){

            return Maps.newHashMap();
        }

        Map<String,String> params = new LinkedHashMap<>(parameterMap.size());

        for(Map.Entry<String,String[]> entry : parameterMap.entrySet()){

            params.put(String.valueOf(entry.getKey()),String.valueOf(entry.getValue()[0]));
        }

        return params;
    }

}
